<template>
   <el-dialog v-model="dialogShow13" width="60%">
          <div class="titleShow">
            <div class="titles">{{ $t('licence.sys_selfTest') }}</div>
          </div>
          <el-table :data="selfTestList" border stripe table-layout="fixed" :show-header="false" style="margin-top:20px">
            <el-table-column prop="label"></el-table-column>
            <el-table-column prop="data"></el-table-column>
          </el-table>
          <template #footer>
            <span class="dialog-footer">
              <el-button @click="dialogShow13 = false">{{ $t('licence.sys_startTest') }}</el-button>
              <el-button type="primary"
                @click="dialogClick13Check()">{{ $t('licence.sys_checkResult') }}</el-button>
            </span>
          </template>
        </el-dialog>
        <!-- 自检报告 -->
        <el-dialog v-model="dialogShow13Check" width="30%">
          <div class="titleShow">
            <div class="titles">{{ $t('licence.sys_checkResult') }}</div>
          </div>
          <el-button type="primary" @click="exportToCSV">{{
            $t('licence.sta_export')
          }}</el-button>
          <el-table :data="testReport" border stripe table-layout="fixed" :show-header="false" style="margin-top:20px">
            <el-table-column prop="label"></el-table-column>
            <el-table-column prop="data"></el-table-column>
            <el-table-column prop="Result"></el-table-column>
          </el-table>
          <template #footer>
            <span class="dialog-footer">
              <el-button type="primary" @click="dialogShow13Check = false">{{ $t('licence.determine') }}</el-button>
            </span>
          </template>
        </el-dialog>
</template>

<script setup lang="ts">
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
const dialogShow13 = ref(false) //自检设置
const dialogShow13Check = ref(false) //自检设置
const selfTestList = [
  {
    label: t("licence.inv_type4"),
    data: '230.2V'
  }, {
    label: t("licence.inv_type5"),
    data: '3.47A'
  }, {
    label: t("licence.inv_type0"),
    data: '798W'
  }, {
    label: t("licence.sys_pl"),
    data: '50.01Hz'
  }
]
const testReport = [{
  label: '59.S2 threshold 264.5V 200ms',
  data: '59.S2:266.0V 212ms',
  Result: 'pass'
}, {
  label: '27.S1 threshold 184V 400ms',
  data: '27.S1:183.4V 403ms',
  Result: 'pass'
}, {
  label: '81>.S2 threshold 51.5Hz 100ms',
  data: '81>.S2 51.51Hz 92ms',
  Result: 'pass'
}, {
  label: '81<.S2 threshold 47.5Hz 100ms',
  data: '81<.S2 47.49Hz 95ms',
  Result: 'pass'
}]
const dialogClick13Check = () => {
  dialogShow13Check.value = true;
}
const convertArrayOfObjectsToCSV = (data: any) => {
  const csvRows = [];
  const headers = Object.keys(data[0]);
  // const headers = ['Setting point','Self-test','Result'];
  // 添加表头
  csvRows.push(headers.join(','));
  // 添加表格数据
  for (const row of data) {
    const values = headers.map(header => {
      const escapeQuotes = row[header]
      return `"${escapeQuotes}"`;
    });
    csvRows.push(values.join(','));
  }

  // 返回CSV内容
  return csvRows.join('\n');
}
// 将自检报告导出为csv
const exportToCSV = () => {
  // 将表格数据转化为CSV格式
  let keyMapping = {}
  if (localStorage.getItem("locale") == "en") {
    keyMapping = {
      label: 'Setting point',
      data: 'Self-test',
      Result: 'Result',
    }
  } else {
    keyMapping = {
      label: '设置点',
      data: '自检结果',
      Result: '状态',
    }
  }

  const modifiedTableData: any = []
  testReport.map(obj => {
    const modifiedObj = {};
    for (let key in obj) {
      if (keyMapping.hasOwnProperty(key)) {
        const newKey = keyMapping[key];
        modifiedObj[newKey] = obj[key];
      } else {
        modifiedObj[key] = obj[key];
      }
    }
    modifiedTableData.push(modifiedObj)
  });
  const csvContent = convertArrayOfObjectsToCSV(modifiedTableData);
  // 创建下载链接，用于下载生成的CSV文件
  const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
  const link = document.createElement('a');
  if (link.download !== undefined) {
    const url = URL.createObjectURL(blob);
    link.setAttribute('href', url);
    link.setAttribute('download', 'data.csv');
    link.style.visibility = 'hidden';
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  }
}
const openDialog = async () => {
  dialogShow13.value = true;
}
defineExpose({
  openDialog
})
</script>